﻿Public Class mainForm

    Private _PWD As String = "Qwe123"

    Private Sub Form1_Load(sender As Object, e As EventArgs) Handles MyBase.Load

        openALL()

        tmServer.Start()
    End Sub

    Private Sub openTMJ()

        Dim isTMJOpen As Boolean = False
        For Each i In Process.GetProcessesByName("Server_TMJ")
            isTMJOpen = True
        Next
        Try
            If isTMJOpen = False Then
                Dim sysPath As String = Application.StartupPath.Replace("Server_Main", "Server_TMJ") & "\Server_TMJ.exe"
                Shell(sysPath)
            End If
            txtTMJ.Visible = True
            txtTMJ.BackColor = Color.LightGreen
            txtTMJ.Text = "脱模机 监控中......"
        Catch ex As Exception
            txtTMJ.Visible = True
            txtTMJ.BackColor = Color.Red
            txtTMJ.Text = "开启 脱模机监控 出错！"
        End Try

    End Sub

    Private Sub btnClose_Click(sender As Object, e As EventArgs) Handles btnClose.Click

        If txtPWD.Text = "" OrElse txtPWD.Text <> _PWD Then
            MsgBox("输入正确密码后才可以关闭服务！", MsgBoxStyle.Exclamation)
            txtPWD.SelectAll()
            txtPWD.Focus()
            Exit Sub
        End If
        closeALL()
        tmServer.Stop()
    End Sub

    Private Sub closeAGV()
        For Each ips In Process.GetProcessesByName("Server_AGV")
            ips.Kill()
            txtAGV.Visible = True
            txtAGV.BackColor = Color.Red
            txtAGV.Text = "AGV监控 已关闭"
        Next
    End Sub

    Private Sub closeTMJ()
        For Each ips In Process.GetProcessesByName("Server_TMJ")
            ips.Kill()
            txtTMJ.Visible = True
            txtTMJ.BackColor = Color.Red
            txtTMJ.Text = "脱模机监控 已关闭"
        Next
    End Sub

    Private Sub closeLHJ()
        For Each ips In Process.GetProcessesByName("Server_LHJ")
            ips.Kill()
            txtLHJ.Visible = True
            txtLHJ.BackColor = Color.Red
            txtLHJ.Text = "硫化机监控 已关闭"
        Next
    End Sub

    Private Sub openLHJ()

        Dim isLHJOpen As Boolean = False
        For Each i In Process.GetProcessesByName("Server_LHJ")
            isLHJOpen = True
        Next
        Try
            If isLHJOpen = False Then
                Dim sysPath As String = Application.StartupPath.Replace("Server_Main", "Server_LHJ") & "\Server_LHJ.exe"
                Shell(sysPath)
            End If
            txtLHJ.Visible = True
            txtLHJ.BackColor = Color.LightGreen
            txtLHJ.Text = "硫化机 监控中......"

        Catch ex As Exception
            txtLHJ.Visible = True
            txtLHJ.BackColor = Color.Red
            txtLHJ.Text = "开启 硫化机监控 出错！"
        End Try
    End Sub

    Private Sub openAGV()

        Dim isAGVOpen As Boolean = False
        For Each i In Process.GetProcessesByName("Server_AGV")
            isAGVOpen = True
        Next
        Try
            If isAGVOpen = False Then
                Dim sysPath As String = Application.StartupPath.Replace("Server_Main", "Server_AGV") & "\Server_AGV.exe"
                Shell(sysPath)
            End If
            txtAGV.Visible = True
            txtAGV.BackColor = Color.LightGreen
            txtAGV.Text = "AGV 监控中......"

        Catch ex As Exception
            txtAGV.Visible = True
            txtAGV.BackColor = Color.Red
            txtAGV.Text = "开启 AGV监控 出错！"
        End Try
    End Sub

    Private Sub tmServer_Tick(sender As Object, e As EventArgs) Handles tmServer.Tick
        If chkOpen.Checked = True Then
            openALL()
        End If
    End Sub

    Private Sub btnReset_Click(sender As Object, e As EventArgs) Handles btnReset.Click
        If txtPWD.Text = "" OrElse txtPWD.Text <> _PWD Then
            MsgBox("输入正确密码后才可以重启服务！", MsgBoxStyle.Exclamation)
            txtPWD.SelectAll()
            txtPWD.Focus()
            Exit Sub
        End If
        tmServer.Stop()
        closeALL()
        Me.Refresh()
        System.Threading.Thread.Sleep(1000)
        openALL()
        tmServer.Start()
    End Sub

    Private Sub mainForm_FormClosing(sender As Object, e As FormClosingEventArgs) Handles Me.FormClosing
        If txtPWD.Text = "" OrElse txtPWD.Text <> "Qwe123" Then
            MsgBox("输入正确密码后才可以重启服务！", MsgBoxStyle.Exclamation)
            txtPWD.SelectAll()
            txtPWD.Focus()
            e.Cancel = True
        Else
            closeALL()
            e.Cancel = False
        End If
    End Sub

    Private Sub closeALL()
        closeTMJ()
        closeAGV()
        closeLHJ()
    End Sub
    Private Sub openALL()
        openAGV()
        openTMJ()
        openLHJ()
    End Sub

    Private Sub btnCloseTMJ_Click(sender As Object, e As EventArgs) Handles btnCloseTMJ.Click
        If txtPWD.Text = "" OrElse txtPWD.Text <> _PWD Then
            MsgBox("输入正确密码后才可以重启服务！", MsgBoxStyle.Exclamation)
            txtPWD.SelectAll()
            txtPWD.Focus()
            Exit Sub
        End If
        If MsgBox("是否关闭脱模机服务。关闭后将不再监控脱模机！", MsgBoxStyle.YesNo, "询问") = MsgBoxResult.Yes Then
            closeTMJ()
            chkOpen.Checked = False
        End If
    End Sub

    Private Sub btnCloseLHJ_Click(sender As Object, e As EventArgs) Handles btnCloseLHJ.Click
        If txtPWD.Text = "" OrElse txtPWD.Text <> _PWD Then
            MsgBox("输入正确密码后才可以重启服务！", MsgBoxStyle.Exclamation)
            txtPWD.SelectAll()
            txtPWD.Focus()
            Exit Sub
        End If
        If MsgBox("是否关闭硫化机服务。关闭后将不再监控硫化机！", MsgBoxStyle.YesNo, "询问") = MsgBoxResult.Yes Then
            closeLHJ()
            chkOpen.Checked = False
        End If

    End Sub

    Private Sub btnCloseAGV_Click(sender As Object, e As EventArgs) Handles btnCloseAGV.Click
        If txtPWD.Text = "" OrElse txtPWD.Text <> _PWD Then
            MsgBox("输入正确密码后才可以重启服务！", MsgBoxStyle.Exclamation)
            txtPWD.SelectAll()
            txtPWD.Focus()
            Exit Sub
        End If
        If MsgBox("是否关闭AGV服务。关闭后将不再监控AGV！", MsgBoxStyle.YesNo, "询问") = MsgBoxResult.Yes Then
            closeAGV()
            chkOpen.Checked = False
        End If
    End Sub
End Class
